<?php
class Back_TournoisController extends Osml_Controller_Admin_Action
{

    public function indexAction()
    {
        $model = new Back_Model_Tournois();
        $select = new Zend_Db_Table_Select($model->_getMapper()->_getTable());
        
        $form = null;
        
        $this->view->tableIdField = 'id_tournois';
        $this->view->tableSelect = $select;
        $this->view->tableHeader = array(
            'nom' => array(
                'name' => 'nom', 
                'link' => array(
                    'action' => 'editer'
                )
            ), 
            'lieu' => array(
                'name' => __("Lieu"), 
                'link' => array(
                    'action' => 'editer'
                )
            ), 
            'date' => array(
                'name' => __("Date"), 
                'link' => array(
                    'action' => 'editer'
                )
            ), 
            'ouvert' => array(
                'name' => __("Ouvert"), 
                'link' => array(
                    'action' => 'editer'
                )
            )
        );
        
        /*
         * Thierry BROUARD (BIIS) callback = array( $objet, $method ) link =
         * array( action [,controller[,module]] ) ou array( 'callback' => array(
         * $objet, $method ) )
         */
        $this->view->tableOptions = array(
            'callback' => array(
                $this, 
                '_transformRowsCallback'
            ),  // Transformation callback
            'link' => array(
                'action' => 'voir'
            ), 
            'pageNumber' => (int) $this->_getParam('page', 1), 
            'pageItemPerPage' => (int) $this->_getParam('par', 10), 
            'form' => $form
        );
    }

    public function editerAction()
    {
        $requete = $this->getRequest();
        $id = $requete->getParam("id", 0);
        
        $form = new Back_Form_Tournois('Formulairejndpro');
        $model = new Back_Model_Tournois($id);
        
        if ($requete->isPost()) {
            if ($form->isValid($requete->getPost())) {
                $date = date(Jnd_Global_Constantes::FORMAT_DATE_BASE);
                if (intval($id) > 0) {
                    $model->charger($id);
                    $model->setOptions($form->getValues());
                } else {
                    $model->setOptions($form->getValues());
                }
                $resultat = $model->sauver();
                $id = $model->getId();
                
                return $this->_helper->redirector('index');
            }
        } elseif ($requete->isGet()) {
            if ($id > 0) {
                $model->charger($id);
                $form->populate($model->toArray());
            }
        }
        $this->view->form = $form;
        $this->view->model = $model;
    }

    public function supprimerAction()
    {
        $request = $this->getRequest();
        $id = $request->getParam("id", 0);
        
        if ($id > 0) {
            $model = new Back_Model_Tournois($id);
        }
    }

    /**
     *
     * @see Jnd_Controller_Action::_transformRowsCallback()
     */
    public function _transformRowsCallback( $row, $view )
    {
        if (isset($row["ouvert"]) && (bool) $row["ouvert"] === true) {
            $row['ouvert'] = __("Oui");
        } elseif (isset($row['ouvert'])) {
            $row['ouvert'] = __("Non");
        }
    }

    public function inscriptionsAction()
    {
        $model = new Back_Model_UtilisateurTournoi();
        $select = new Zend_Db_Table_Select($model->_getMapper()->_getTable());
        
        $form = null;
        
        $this->view->tableIdField = 'id_liaison';
        $this->view->tableSelect = $select;
        $this->view->tableHeader = array(
            'id_tournoi' => array(
                'name' => 'tournoi', 
                'link' => array(
                    'action' => 'editer'
                )
            ), 
            'id_utilisateur' => array(
                'name' => __("utilisateur"), 
                'link' => array(
                    'module' => 'back',
                    'controller' => 'utilisateurs',
                    'action' => 'editer'
                )
            ), 
            'nom' => array(
                'name' => __("nom"), 
                'link' => array(
                    'action' => 'editer'
                )
            ), 
            'prenom' => array(
                'name' => __("prénom"), 
                'link' => array(
                    'action' => 'editer'
                )
            ), 
            'presence' => array(
                'name' => __("présence"), 
                'link' => array(
                    'action' => 'editer'
                )
            ), 
            'apporte' => array(
                'name' => __("apporte"), 
                'link' => array(
                    'action' => 'editer'
                )
            )
        );
        
        /*
         * Thierry BROUARD (BIIS) callback = array( $objet, $method ) link =
         * array( action [,controller[,module]] ) ou array( 'callback' => array(
         * $objet, $method ) )
         */
        $this->view->tableOptions = array(
            'callback' => array(
                $this, 
                '_transformRowsCallback'
            ),  // Transformation callback
            'link' => array(
                'action' => 'voir'
            ), 
            'pageNumber' => (int) $this->_getParam('page', 1), 
            'pageItemPerPage' => (int) $this->_getParam('par', 10), 
            'form' => $form
        );
    }
}